/* RegistrationService.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package services;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import repositories.RegistrationRepository;
import security.LoginService;
import security.UserAccount;

import domain.Announcement;
import domain.Customer;
import domain.Registration;

@Service
@Transactional
public class RegistrationService {
	
	// Managed repository -----------------------------------------------------
	@Autowired
	RegistrationRepository registrationRepository;
	// TODO: Add managed repository.
	
	// Supporting services ----------------------------------------------------
	@Autowired
	AnnouncementService announcementService;
	
	@Autowired
	CustomerService customerService;
	// TODO: Add supporting services.
	
	// Constructors -----------------------------------------------------------

	public RegistrationService() {
		super();
	}
	
	// Simple CRUD methods ----------------------------------------------------
	
	public Registration createRegistration(Announcement announcement) {
		Registration result;
		
		// TODO: Complete this method. 
		assert announcement != null;
		Customer customer = customerService.findByPrincipal();
		result = new Registration();
		result.setMoment(new Date());
		result.setAnnouncement(announcement);
		result.setOwner(customer);
		return result;
	}

	public void save(Registration registration) {
		// TODO: Complete this method. 
		assert registration != null;
		registration.setMoment(new Date());
		registrationRepository.save(registration);
	}
	
	public void delete(Registration registration) {
		// TODO: Complete this method.
		assert registration != null;
		registrationRepository.delete(registration);
	}
	
	// Business methods -------------------------------------------------------

	public Collection<Registration> findByPrincipal() {
		Collection<Registration> result;
		
		// TODO: Complete this method.
		UserAccount userAccount;
		userAccount = LoginService.getPrincipal();
		assert userAccount != null;
		Customer c = customerService.findByUserAccount(userAccount);
		result = registrationRepository.findByCustomerId(c.getId());

		return result;
	}
	
	public boolean existsRegistrationForAnnouncement(Announcement announcement) {
		boolean result;
		
		// TODO: Complete this method. 
		Collection<Registration> c = registrationRepository.findByAnnouncement(announcement);
		if(c.size() != 0)
			result = true;
		else
			result = false;
		return result;
	}

	public Registration findByCustomerAndAnnouncement(Customer customer, Announcement announcement) {
		Registration result;

		// TODO: Complete this method. 
		result = registrationRepository.findByCustomerAndAnnouncement(customer, announcement);
						
		return result;
	}

}
